const randomChars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz123456789';
const utils = {
    // 获取ajax response 大小
    getResponseSize(xhr){
        let size = 0;
        if(xhr.getResponseHeader("Content-Length") !== null){
            return parseInt(xhr.getResponseHeader("Content-Length"),10);
        }
        const responseType = xhr.responseType;
        switch(responseType){
            case ""://DOMString
            case "text"://DOMString
                let txt = xhr.responseText;
                return utils.string2Bytes(txt);
            case "blob"://Blob
                return xhr.response.size;
            case "json"://JavaScript object, parsed from a JSON string returned by the server
                let jsonStr = JSON.stringify(xhr.response);
                return utils.string2Bytes(jsonStr);
            case "arraybuffer"://ArrayBuffer对象
                return xhr.response.byteLength;
            case "document"://document xml
                let xmlStr = utils.XML2String(xhr.responseXML);
                return utils.string2Bytes(xmlStr);
            default:
                break;
        }
        return size;
    },
    /**
     * 生成随机字符串
     * @param {Number} len 
     */
    randomString(len) {
    　　len = len || 10;
        const maxPos = randomChars.length;
        let pwd = '';
    　　for (let i = 0; i < len; i++) {
    　　　　pwd = pwd + randomChars.charAt(Math.floor(Math.random() * maxPos));
    　　}
    　　return pwd + new Date().getTime();
    },
    /**
     * 生成guid
     */
    guid() {
        let d = new Date().getTime();
        const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
            let r = (d + Math.random()*16)%16 | 0;
            d = Math.floor(d/16);
            return (c=='x' ? r : (r&0x3|0x8)).toString(16);
        });
        return uuid;
    },
    /** 
     * 计算字符串所占的内存字节数，默认使用UTF-8的编码方式计算，也可制定为UTF-16
     * @param  {String} str 
     * @param  {String} charset utf-8, utf-16
     * @return {Number}
     */
    string2Bytes(str, charset){
        let total = 0,
            charCode,
            i,
            len;
        charset = charset ? charset.toLowerCase() : '';
        if(charset === 'utf-16' || charset === 'utf16'){
            for(i = 0, len = str.length; i < len; i++){
                charCode = str.charCodeAt(i);
                if(charCode <= 0xffff){
                    total += 2;
                }else{
                    total += 4;
                }
            }
        }else{
            for(i = 0, len = str.length; i < len; i++){
                charCode = str.charCodeAt(i);
                if(charCode <= 0x007f) {
                    total += 1;
                }else if(charCode <= 0x07ff){
                    total += 2;
                }else if(charCode <= 0xffff){
                    total += 3;
                }else{
                    total += 4;
                }
            }
        }
        return total;
    },
    // xml转string
    XML2String(xmlObject) {
        if (window.ActiveXObject) {      
          return xmlobject.xml;
        }
        else {       
          return (new XMLSerializer()).serializeToString(xmlobject);
        }
    },
    // 获得 uuid
    getUuid(){
        let markUser = localStorage.getItem('_ps_mark_uuid_')||'';
        if(!markUser){
            markUser = utils.guid();
            localStorage.setItem('_ps_mark_uuid_',markUser);
        }
        return markUser;
    },
    // 获得mark uuid
    markUuid(){
        let markUser = sessionStorage.getItem('_ps_mark_uuid_')||'';
        if(!markUser){
            markUser = utils.guid();
            sessionStorage.setItem('_ps_mark_uuid_',markUser);
        }
        return markUser;
    },
    // 获得Uv
    markUv(){
        const date = new Date();
        let markUv = localStorage.getItem('_ps_markUv_')||'';
        const datatime = localStorage.getItem('_ps_markUvTime_')||'';
        const today = date.getFullYear()+'/'+(date.getMonth()+1)+'/'+date.getDate()+' 23:59:59';
        if( (!markUv && !datatime) || (date.getTime() > datatime*1) ){
            markUv = utils.guid();
            localStorage.setItem('_ps_markUv_',markUv);
            localStorage.setItem('_ps_markUvTime_',new Date(today).getTime());
        }
        return markUv;
    }
}

export default utils;